package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"srv/models/mysql"

	"srv/house"
	"srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseAddLogic {
	return &HouseAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseAddLogic) HouseAdd(in *house.HouseAddRequest) (*house.HouseAddResponse, error) {
	// todo: add your logic here and delete this line
	h := mysql.House{
		Model:   gorm.Model{},
		Name:    in.Name,
		Address: in.Address,
		Type:    in.Type,
		Area:    in.Area,
		Floor:   in.Floor,
		Price:   float64(in.Price),
		Remarks: in.Remarks,
	}
	err := mysql.DB.Debug().Create(&h).Error
	if err != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &house.HouseAddResponse{
		Id: int64(h.ID),
	}, nil
}
